package jp.co.kayo.gae.takidashibot.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import jp.co.kayo.gae.takidashibot.model.Takidasi;
import jp.co.kayo.gae.takidashibot.utils.Utils;

public class Henjitter {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm");
    DecimalFormat distFormat = new DecimalFormat("0.00");
    
    public Henjitter(){
		TimeZone tz = TimeZone.getTimeZone("Asia/Tokyo");
	    sdf.setTimeZone(tz);
    }

    public String kokodayo(String scname, Takidasi takidasi, double lat, double lon, double m){
	    StringBuilder buf = new StringBuilder();
	    buf.append("@").append(scname).append(" ");
        buf.append(takidasi.getName()).append(" ");
        buf.append(" ").append(distFormat.format(m)).append("km ");
        StringBuilder urlstr = new StringBuilder();
        //urlstr.append("http://maps.google.com/maps?q=").append(lat).append(",").append(lon).append("+(").append(URLEncoder.encode(title, "UTF-8")).append(")");
        urlstr.append("http://takidasibot.appspot.com/takidasibot/item.html?id=").append(takidasi.getId());
        
        String url = Utils.urlShorter(urlstr.toString());
        int fixlength = (140-(url+" ").length());
        
        String addr = takidasi.getAddr();
        
        boolean bs = false;
        if(addr!=null){
            Utils.addDescription(buf, addr, fixlength);
            bs = true;
        }
        if(takidasi.getDescription()!=null){
            if(bs) buf.append(" ");
            String rep = takidasi.getDescription().replace("@", "");
            Utils.addDescription(buf, rep, fixlength-1);
        }
        buf.append(" ").append(url);
        return buf.toString();
	}
}
